package org.example.controller.SpringBoot300.ut;

import org.example.util.file.FileUtil;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * @auther admin闫
 * @Descriptopn 抖音模板背景图生成脚本
 * @date 2024/4/6 2:55
 */
public class DYMoBan {

    public static void main(String[] args) {
        List<String> allFilePaths = FileUtil.getAllFilePaths("H:\\短视频工作目录\\新建文件夹 (2)");
        int i=0;
        for (String allFilePath : allFilePaths) {
            i++;
            try {
                // 加载源JPEG图像
                BufferedImage jpgImage = ImageIO.read(new File(allFilePath));
                // 加载目标PNG背景图像
                BufferedImage pngBackground = ImageIO.read(new File("H:\\毕业设计工作目录\\batch_processing\\batch_processing\\测试\\三连封面模板.png"));

                // 获取PNG图像的宽度和高度
                int width = pngBackground.getWidth();
                int height = pngBackground.getHeight();

                // 创建一个新的BufferedImage，用于合成最终图像，大小与PNG背景图像相同
                BufferedImage combinedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
                Graphics2D g = combinedImage.createGraphics();
                BufferedImage bufferedImage = resizeImage(jpgImage, width, height);
                // 绘制JPEG图像
                g.drawImage(bufferedImage, 0, 0, null);
                // 绘制PNG背景，覆盖在JPEG图像之上
                g.drawImage(pngBackground, 0, 0, null);
                // 释放资源
                g.dispose();
                // 保存合成后的图像为新的PNG文件
                ImageIO.write(combinedImage, "PNG", new File("H:\\毕业设计工作目录\\batch_processing\\batch_processing\\封面模板\\"+i+".png"));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    // 调整图像大小
    private static BufferedImage resizeImage(BufferedImage originalImage, int targetWidth, int targetHeight) {
        Image resultingImage = originalImage.getScaledInstance(targetWidth, targetHeight, Image.SCALE_SMOOTH);
        BufferedImage outputImage = new BufferedImage(targetWidth, targetHeight, BufferedImage.TYPE_INT_ARGB);
        Graphics2D g = outputImage.createGraphics();
        g.drawImage(resultingImage, 0, 0, null);
        g.dispose();
        return outputImage;
    }

}
